package com.ating.demo.Service;

import cn.hutool.core.util.StrUtil;
import com.ating.demo.Entity.Admin;
import com.ating.demo.Entity.Dto;
import com.ating.demo.Entity.Employee;
import com.ating.demo.Exceptiom.CustomException;
import com.ating.demo.Mapper.AdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {

    @Resource
    private AdminMapper adminMapper;

    public List<Admin> selectAll(Admin admin){
        return adminMapper.selectAll(admin);
    }

    public Admin selectOne(Integer id){
        return adminMapper.selectById(id);
    }

    public PageInfo<Admin> selectPage(Integer PageSize, Integer PageNum, Admin admin){
        PageHelper.startPage(PageNum,PageSize);
        List<Admin> list = adminMapper.selectAll(admin);
        return PageInfo.of(list);
    }

    public void add(Admin admin){
        String username = admin.getUsername();
        Admin admin1 = adminMapper.selectByUsername(username);
        if(admin1 != null){
            throw new CustomException("500","账号已存在");
        }
        //如果名字没填
        if(StrUtil.isBlank(admin.getName())){
            admin.setUsername(admin.getUsername());
        }
        admin.setRole("ADMIN");
        adminMapper.insert(admin);
    }

    public void update(Admin admin){
        adminMapper.updateById(admin);
    }

    public Admin login(Dto dto){
        String username = dto.getUsername();
        Admin admin1 = adminMapper.selectByUsername(username);
        //先查询用户在不在
        if(admin1 == null){
            throw new CustomException("500","查询不到该用户");
        }
        //查询密码正不正确
        String password = dto.getPassword();
        if(!admin1.getPassword().equals(password)){
            throw new CustomException("500","账号或密码错误");
        }
        return admin1;
    }

    public void moveById(Integer id){
        adminMapper.moveById(id);
    }
}
